//
//  PicViewCell.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/25.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit
//设置图片信息
class PicViewCell: UICollectionViewCell
{

    //懒加载UIImageView
    lazy var picImage:UIImageView = {
       
        let img = UIImageView(imgName: "weibo_placeholder")
        
        //设置图片填充???
        img.contentMode = .ScaleAspectFill
        img.layer.masksToBounds = true
        
        
        return img
        
    }()
    
    //懒加载GIF图标
     private lazy var gifImageView: UIImageView = UIImageView(imgName: "timeline_image_gif")
    
    
    //从外界获取到的图片数据之后，对相关图片控件进行重新设置
    var  picModel:PictureModel?{
        
        didSet{
            
            //判断获取到的数据
            guard let model = picModel else
            {
                return
            }
            
            //获取图片的地址赋值给picImage
            self.picImage.sh_setImage(model.thumbnail_pic, placeholderImgName: "weibo_placeholder")
            
            
            //判断图片是不是GIF图片
            if let picurl = model.thumbnail_pic where picurl.hasSuffix(".gif")
            {
                //显示
                self.gifImageView.hidden = false
            }
            else
            {
                //隐藏
                self.gifImageView.hidden = true
            }
            
        }
        
    }
    
    
    
    //重写init()方法,设置cell上的微博图片信息
    //重写init方法用来设置图片
    override init(frame: CGRect)
    {
        
        super.init(frame: frame)
        
        //重新设置图片
        setUpUI()
    }
    
    
    
    func setUpUI()
    {
               
        //添加图片控件
        addSubview(self.picImage)
        //添加GIF图标
        addSubview(self.gifImageView)
        
        
        
        //设置图片的约束
        self.picImage.snp_makeConstraints { (make) in
            
            //设置边距全为0
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        //设置GIF图片的约束
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
    }
    
    
    
    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
